package GUI;

import java.awt.Rectangle;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import Client_logic.ClientNotInitializesException;
import Client_logic.ConnectionToServer;

/**
 * This class use for show and update residencesArea details. 
 * @author ido
 * @author michael
 * @author matan
 * @author sharon
 * 
 *
 */
public class ResidencesAreaForm extends JPanel
{
	private JTextField areaIDTextField = null;
	private JTextField areaNameTextField = null;
	private JLabel allAreaLabel= null;
	private JLabel areaIDLabel = null;
	private JLabel areaNameLabel = null;
	private JButton addButton = null;
	private JButton updateButton = null;
	private JButton cancelButton = null;	
	private JButton refreshButton = null;
	private JComboBox allAreaJComboBox= null;
	
	/**
	 * This is the default constructor
	 */
	public ResidencesAreaForm() 
	{
		super();
		initialize();	
	}
	
	/**
	 * This method initializes CustomerForm
	 */
	private void initialize() 
	{
		allAreaLabel = new JLabel();
		allAreaLabel.setBounds(new Rectangle(15, 20, 130, 22));
		allAreaLabel.setText("All areas:");
		
		areaIDLabel = new JLabel();
		areaIDLabel.setBounds(new Rectangle(15, 70, 130, 22));
		areaIDLabel.setText("Area ID:");
		
		areaNameLabel = new JLabel();
		areaNameLabel.setBounds(new Rectangle(15, 100, 130, 22));
		areaNameLabel.setText("Area Name:");
		
		this.setSize(300, 220);
		this.setLayout(null);
		this.add(allAreaLabel, null);
		this.add(areaIDLabel, null);
		this.add(areaNameLabel, null);
		this.add(getAreaIDTextField(), null);
		this.add(getAreaNameTextField(), null);
		this.add(getAllAreaJComboBox(), null);
		this.add(getCancelButton(),null);
		this.add(getRefreshButton(),null);		
	}
	
	/**
	 * This method initializes areaIDTextField	
	 */
	private JTextField getAreaIDTextField()
	{
		if (areaIDTextField == null) 
		{
			areaIDTextField = new JTextField();
			areaIDTextField.setBounds(new Rectangle(150, 70, 127, 25));
		}
		return areaIDTextField;
	}
	
	/**
	 * This method initializes areaNameTextField	
	 */
	private JTextField getAreaNameTextField()
	{
		if (areaNameTextField == null) 
		{
			areaNameTextField = new JTextField();
			areaNameTextField.setBounds(new Rectangle(150, 100, 127, 25));
		}
		return areaNameTextField;
	}
	
		
	/**
	 * This method initializes allAreaJComboBox	
	 */
	private JComboBox getAllAreaJComboBox()
	{
		if (allAreaJComboBox == null) 
		{
			allAreaJComboBox = new JComboBox();
			allAreaJComboBox.setBounds(new Rectangle(150, 20, 127, 25));
			updateallAreaJComboBox();			
		}
		return allAreaJComboBox;
	}
	
	/**
	 * This method update allAreaJComboBox 
	 */
	private void updateallAreaJComboBox()
	{
		try
		{
			int i=0,itemcount = allAreaJComboBox.getItemCount();
			if (itemcount > 0) 
			{
				allAreaJComboBox.addItem(":");
				allAreaJComboBox.setSelectedItem(":");				
			}
			for (i=0; i < itemcount ; i++ )
			{				
				allAreaJComboBox.removeItemAt(0);						
			}					
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "ERROR!!!","",JOptionPane.ERROR_MESSAGE);
		}			
		String[] areaList = null;
		try
		{
			ConnectionToServer connectiontoserver = new ConnectionToServer();		
			areaList = (String[]) connectiontoserver.loadAllAreas();
			if (areaList == null)
			{
				JOptionPane.showMessageDialog(null, "ERROR!!!");
				allAreaJComboBox.addItem(":");
			}	
			else
			{
				int len = areaList.length;
				if (len == 0)
					allAreaJComboBox.addItem("0:EMPTY");						
				for(int i=0; i < len; i++)
				{
					allAreaJComboBox.addItem(areaList[i]);
				}	
			}
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "ERROR!!!");
			allAreaJComboBox.addItem(":");
		}			
	}
	
	/**
	 * This method update TextFields to area details
	 */
	private void  updateTextField()
	{
		String area = (String)allAreaJComboBox.getSelectedItem();
		areaIDTextField.setText(area.substring(0, area.indexOf(":")));	
		areaNameTextField.setText(area.substring(area.indexOf(":")+1, area.length()));			
	}
	
	/**
	 * This method initializes cancelButton
	 */	
	public JButton getCancelButton()
	{
		if (cancelButton == null)
		{
			cancelButton = new JButton();
			cancelButton.setText("Cancel");
			cancelButton.setBounds(new Rectangle(15, 130, 80, 29));
			cancelButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					//close
				}				
			});						
		}		
		return cancelButton;				
	}
	
	/**
	 * This method initializes SaveButton	
	 */
	private JButton getSaveButton() 
	{
		if (addButton == null) 
		{
			addButton = new JButton();
			addButton.setBounds(new Rectangle(195, 130, 80, 29));
			addButton.setText("ADD");
			addButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					saveArea();
				}					
			});						
		}
		return addButton;
	}	
	
	/**
	 * This method update TextFields to area details
	 */
	private void  saveArea()
	{		
		Object msgfromsetrver = null;
		try
		{
			int areaID = Integer.parseInt(areaIDTextField.getText());	
			String areaName = areaNameTextField.getText();
			ConnectionToServer connectiontoserver = new ConnectionToServer();
			msgfromsetrver = (String)connectiontoserver.addArea(areaID, areaName);
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);		
		}
		if (msgfromsetrver != null)
		{
			if (msgfromsetrver.equals("OK"))
				JOptionPane.showMessageDialog(null, "The area successfully save");
			else
				JOptionPane.showMessageDialog(null, "Ilega area number","WARNING", JOptionPane.WARNING_MESSAGE);
		}					
	}
	
	/**
	 * This method initializes updateButton	
	 */
	private JButton getUpdateButton() 
	{
		if (updateButton == null) 
		{
			updateButton = new JButton();
			updateButton.setBounds(new Rectangle(195, 130, 80, 29));
			updateButton.setText("Update");
			updateButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					updateArea();
				}					
			});						
		}
		return updateButton;
	}
	
	/**
	 * This method update area details in data base
	 */
	private void  updateArea()
	{		
		int areaID = Integer.parseInt(areaIDTextField.getText());	
		String areaName = areaNameTextField.getText();
		Object msgfromsetrver = null;
		try
		{
			ConnectionToServer connectiontoserver = new ConnectionToServer();
			msgfromsetrver = (String)connectiontoserver.updateArea(areaID, areaName);
		}catch(Exception e)
		{
			JOptionPane.showMessageDialog(null, "FATAL ERROR!!!","",JOptionPane.ERROR_MESSAGE);		
		}
		if (msgfromsetrver != null)
		{
			if (msgfromsetrver.equals("OK"))
				JOptionPane.showMessageDialog(null, "The area successfully update");
			else
				JOptionPane.showMessageDialog(null, "The area not update!!!","WARNING", JOptionPane.WARNING_MESSAGE);
		}		
	}
		
	/**
	 * This method initializes refreshButton	
	 */
	private JButton getRefreshButton() 
	{
		if (refreshButton == null) 
		{
			refreshButton = new JButton();
			refreshButton.setBounds(new Rectangle(105, 130, 80, 29));
			refreshButton.setText("Refresh");
			refreshButton.addActionListener(new java.awt.event.ActionListener() 
			{
				public void actionPerformed(java.awt.event.ActionEvent e) 
				{
					areaNameTextField.setText("");
					areaIDTextField.setText("");
					updateallAreaJComboBox();
				}					
			});						
		}
		return refreshButton;
	}
	
	/**
	 * This method add UpdateButton to the FORM
	 */
	public void addUpdateButton()
	{
		this.add(getUpdateButton(),null);
		areaIDTextField.setEditable(false);
		allAreaJComboBox.addActionListener(new java.awt.event.ActionListener() 
		{
			public void actionPerformed(java.awt.event.ActionEvent e) 
			{
				updateTextField();
			}	
		});			
	}
	
	/**
	 * This method add SaveButton to the FORM
	 */
	public void addSaveButton()
	{
		this.add(getSaveButton(),null);
	}
	
}
